package com.kjsdfgk.hjmdsf.register.mvp.presenter;

import android.app.Application;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.SPUtils;
import com.google.gson.JsonObject;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;

import javax.inject.Inject;

import com.jess.arms.utils.PermissionUtil;
import com.jess.arms.utils.RxLifecycleUtils;
import com.kjsdfgk.hjmdsf.app.entity.Account;
import com.kjsdfgk.hjmdsf.app.entity.RegJson;
import com.kjsdfgk.hjmdsf.app.msg.AddViewMsg;
import com.kjsdfgk.hjmdsf.register.mvp.contract.RegisterContract;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.List;
import java.util.UUID;

import static com.kjsdfgk.hjmdsf.app.Config.DATA_PATH;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 04/19/2020 00:41
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class RegisterPresenter extends BasePresenter<RegisterContract.Model, RegisterContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public RegisterPresenter(RegisterContract.Model model, RegisterContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
    public void userReg(int inv , String account, String password) {

        PermissionUtil.externalStorage(new PermissionUtil.RequestPermission() {
            @Override
            public void onRequestPermissionSuccess() {

                try {
                    SPUtils.getInstance().put("account",account);
                    SPUtils.getInstance().put("password",password);
                    File file = new File( DATA_PATH + "tencent.dat");
                    if (file.exists() || true){
                        String device =  FileIOUtils.readFile2String(file);
                        mModel.userReg( inv , account, password , device)
                                .subscribeOn(Schedulers.io())
                                .retryWhen(new RetryWithDelay(3, 2))
                                .subscribeOn(AndroidSchedulers.mainThread())
                                .observeOn(AndroidSchedulers.mainThread())
                                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                                .subscribe(new ErrorHandleSubscriber<RegJson>(mErrorHandler) {
                                    @Override
                                    public void onError(@NonNull Throwable e) {

                                        mRootView.hideLoading();
                                        mRootView.showMessage(e.toString());

                                    }

                                    @Override
                                    public void onNext(@NonNull RegJson regJson) {
                                        mRootView.hideLoading();
                                        if (regJson.getCode() == 200){
                                            mRootView.regSucc();
                                            mRootView.showMessage(regJson.getMsg());
                                            EventBus.getDefault().post(new AddViewMsg("getUserInfo","" ));
                                        }else if (regJson.getCode() == 117){
                                            mRootView.showMessage("该设备已经注册了 ，请联系客服");
                                        }else {
                                            mRootView.showMessage(regJson.getMsg());
                                        }
                                    }
                                });
                    } else {
                       mRootView.showMessage("注册错误 ，请联系客服");
                    }
                }catch (Exception e){
                    //   mRootView.showMessage("写入失败--->"+e.toString());
                }

            }

            @Override
            public void onRequestPermissionFailure(List<String> permissions) {

            }

            @Override
            public void onRequestPermissionFailureWithAskNeverAgain(List<String> permissions) {

            }


        }, mRootView.getRxPermissions(), mErrorHandler);


    }
}
